import re
from flask import Blueprint
from flask import request,jsonify,send_from_directory
from db import Db
import redis
from mypage import Page
from myfile import MyFile
import os
import math
import requests
import json
from shop_del import DelDb
import redis

kaoshi = Blueprint('kaoshi',__name__)

@kaoshi.before_request
def before_request():
    if request.path not in [""]:
        redims = redis.Redis()
        redims.set('flask',"123")
        
@kaoshi.route('/addshop/',methods=['POST'])
def addshop():
    name = request.form.get('name')
    color = request.form.get('color')
    price = request.form.get('price')
    db= Db()
    try:
        db.insert("insert into `shop` (name,color,price) values ('%s','%s','%s')"%(name,color,price)) 
    except Exception as e:
        return {'code':400,'msg':'添加失败'}
    else:
       return {'code':200,'msg':'添加成功'}

@kaoshi.route('/shops/',methods=['GET'])
def shops():
    db = Db()
    res = db.get_all("select * from `shop`") 
    return jsonify(res)


@kaoshi.route('/delshop/',methods=['DELETE'])
def delshop():
    id = request.args.get('id')
    db = DelDb()
    try:
        db.delete("delete from `shop` where id=%s"%id)
    except Exception as e:
        return {'code':400,'msg':'删除失败'}
    else:
       return {'code':200,'msg':'删除成功'}






     
    


      




